<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class Tools_model extends CI_Model {
	function copyfiles($rand, $id, $folder) {
		$path = "./uploads/uploadify/" . $rand . "/";
		$newpath = "./uploads/" . $folder . "/" . $id . "/";
		mkdir($newpath);
		mkdir($newpath . "small");
		mkdir($newpath . "middle");
		chmod($newpath, 0777);
		chmod($newpath . "small", 0777);
		chmod($newpath . "middle", 0777);
		$files = directory_map($path, true);
		foreach ($files as $file) {
			if ($file != "small" && $file != "middle") {
				copy($path . $file, $newpath . $file);
				copy($path . "small/" . $file, $newpath . "small/" . $file);
				copy($path . "middle/" . $file, $newpath . "middle/" . $file);
				$return[] = $file;
				unlink($path."small/".$file);
				unlink($path."middle/".$file);
				unlink($path.$file);
			}
		}
		rmdir($path."middle");
		rmdir($path."small");
		rmdir($path);
		return $return;
	}
	function check($val) {
		$field = sha1("soltbyme".$this->session->userdata('session_id')."myhashsolt");
		$key = $this->input->post($field);
		$code = $this->db->select()->from('captcha')->where('captcha_session', $key)->where('captcha_code', $val)->order_by('captcha_id', 'desc')->get()->row_array();
		if (sizeof($code) > 0) {
			$expire = time() - $code['captcha_date'];
			$this->db->where('captcha_id', $code['captcha_id'])->delete('captcha');
			if ($expire < 600) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	function captcha($form) {
		$key = sha1("soltbyme".$this->session->userdata('session_id')."myhashsolt");

		if ($this->input->post($key) !== false) {
			$rand = $this->input->post($key);
		} else {
			$rand = md5(md5(time().sha1(time()."lalala")));
		}

		$form->addElement('hidden', $key)->setValue($rand);
		$gr = $form->insertGroup()->addClass('col col2');
		$code = $gr->insertInputText(sha1("soltbyme".$this->session->userdata('session_id')."mybigsolthash"))->setLabel('Введите код с картинки')->setComment('Защита от роботов')->setAttribute('autocomplete', 'off');
		$code->addRule('callback', 'Не верный код', array('callback' => array($this->tools_model, 'check')), HTML_QuickForm2_Rule::SERVER);
		$gr->insertStatic("<img src='/thumb/tools/".$rand."/".md5(md5(time().sha1(time())))."/' alt='Dont do that!' title='Dont do that!' />");
		return $form;
	}
	function created_email($string) { // валидация на созданный email
		$check = $this->db->select()->from('users')->where('email', $string)->get()->result_array();
		if (sizeof($check) > 0) return false; else return true;
	}
}
?>